﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WingsOverSpace
{
    class WingsDrawableGameComponent : DrawableGameComponent
    {
        public enum ExplosionOrder { NoExplosion, BeginExplosion, BeginExplosion2, BeginExplosion3, Explosion, Explosion2, EndExplosion, EndExplosion2 }

        public Vector2 Position { get; set; }
        public Matrix SpriteScale;
        public Rectangle boundBox;
        public Boolean toBeDeleted;
        public ExplosionOrder ExplodeOrder = ExplosionOrder.NoExplosion;
        public int timeOnExplosion = 0;

        public Texture2D sprite;
        public int SpriteHeightDivFactor = 1; // for sprite sheets, default 1
        public int SpriteWidthDivFactor = 1; //for sprite sheets
        public float SpriteScaleFactor;

        public WingsDrawableGameComponent(Game g)
            : base(g)
        {
            toBeDeleted = false;
        }

        protected override void LoadContent()
        {
            boundBox = new Rectangle((int) (Position.X - SpriteScaleFactor * sprite.Width / (2 * SpriteWidthDivFactor)),
                (int)(Position.Y - SpriteScaleFactor * sprite.Height / (2 * SpriteHeightDivFactor)),
                (int)(SpriteScaleFactor * sprite.Width / SpriteWidthDivFactor) ,
                (int)(SpriteScaleFactor * sprite.Height / SpriteHeightDivFactor));
            base.LoadContent();
        } 

        public override void Update(GameTime gameTime)
        {
            boundBox = new Rectangle((int)(Position.X - SpriteScaleFactor * sprite.Width / (2 * SpriteWidthDivFactor)),
               (int)(Position.Y - SpriteScaleFactor * sprite.Height / (2 * SpriteHeightDivFactor)),
               (int)(SpriteScaleFactor * sprite.Width / SpriteWidthDivFactor),
               (int)(SpriteScaleFactor * sprite.Height / SpriteHeightDivFactor));

            if (toBeDeleted == true)
            {
                Visible = false;
                toBeDeleted = false;
            }

            base.Update(gameTime);
        }

        public enum ComponentType { Enemy, EnemyShot, Player, PlayerShot }

        //Look for unused objects
        public WingsDrawableGameComponent UnusedObject(ComponentType type)
        {
            foreach (WingsDrawableGameComponent comp in Game.Components)
            {
                switch (type)
                {
                    case ComponentType.Enemy:
                        if (comp is Enemy && comp.Visible == false)
                            return comp;
                        break;
                    case ComponentType.EnemyShot:
                        if (comp is EnemyShot && comp.Visible == false)
                            return comp;
                        break;
                    case ComponentType.PlayerShot:
                        if (comp is PlayerShot && comp.Visible == false)
                            return comp;
                        break;
                    default:
                        return null;
                }
            }

            return null;
        }


    }




}
